﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Config;
using DLL.Entities.Event;

namespace MIS
{
    public partial class MIS : Form

    #region Missile XML read and write
    {
        ReadXML xRead = null;
        WriteXML xWrite = null;
        MISConfig configM = null;

        COMEvent eventC = null;

        int missileCount;
        double distanceRemaining;

        public MIS()
        {
            InitializeComponent();

            xRead = new ReadXML();
            xWrite = new WriteXML();
            configM = xRead.ReadMISConfig();

            missileCount = 25;
        }

        private void MIS_Load(object sender, EventArgs e)
        {
            xWrite.WriteMISStatus(missileCount, configM.MISStatusDir);
            tWriteRead.Enabled = true;
            lblVersionMis.Text = configM.Version.ToString();
            txtMisStatus.Text = "Ready";
        }

        // Sends Offline status to COM when form is closed
        private void MIS_FormClosed(object sender, FormClosedEventArgs e)
        {
            missileCount = 0;
            tWriteRead.Enabled = false;
            xWrite.WriteCOMEvent(missileCount, 0, 0, 0, 0, configM.COMEventDir);
        }

        public void tWriteRead_Tick(object sender, EventArgs e)
        {
            // Send Status
            xWrite.WriteMISStatus(missileCount, configM.MISStatusDir);

            // Read for Event
            ReadEvent();

           xWrite.WriteCOMEvent(0, 0, 0, 0, 0, configM.COMEventDir);
        }

        public void ReadEvent()
        {
            try
            {
                eventC = xRead.ReadCOMEvent(configM.COMEventDir);
                int on = eventC.On;

                if (txtBogieSpeed.Text == "" || txtBogieSpeed.Text == "0")
                {
                    missileCount -= eventC.LessOne;
                    txtAltitude.Text = eventC.Altitude.ToString();
                    txtBogieSpeed.Text = eventC.Speed.ToString();
                    txtDistance.Text = eventC.Distance.ToString();
                    lblMissileRemaining.Text = Convert.ToString(missileCount);
                 
                    if (on.Equals(1))
                    {
                        picboxShortRange.Visible = false;
                        picBoxIncoming.Visible = false;
                        picboxMediumRange.Visible = true;
                        picBoxMissileFail.Visible = false;
                        txtMisStatus.Text = "ENROUTE";

                        tDistance_Timer.Enabled = true;
                        tAltitudeDecrement.Enabled = true;
                        tWriteRead.Interval = 10000;
                    }
                }
                else if (on.Equals(2))
                {
                    picboxMediumRange.Visible = false;
                    picboxShortRange.Visible = false;
                    tDistance_Timer.Enabled = false;
                    tAltitudeDecrement.Enabled = false;
                    txtAltitude.Text = "0";
                    txtBogieSpeed.Text = "0";
                    txtDistance.Text = "0";
                    txtMisStatus.Text = "Ready";
                    MessageBox.Show("Missile Aborted");
                    tWriteRead.Interval = 100;
                }
                else if (distanceRemaining <= 0)
                {
                    tAltitudeDecrement.Enabled = false;
                    txtBogieSpeed.Text = "0";
                    txtAltitude.Text = "0";
                    txtDistance.Text = "0";
                    txtMisStatus.Text = "Ready";
                    MessageBox.Show("Target Destroyed");
                    picboxMediumRange.Visible = false;
                }

                //else if (distanceRemaining >= 0)
                //{
                //    tAltitudeDecrement.Enabled = false;
                //    txtBogieSpeed.Text = "0";
                //    txtAltitude.Text = "0";
                //    txtDistance.Text = "0";
                //    txtMisStatus.Text = "Ready";
                //    MessageBox.Show("Missile Aborted");
                //    picboxMediumRange.Visible = false;
                //    tWriteRead.Interval = 100;
                //}

                else
                    picboxShortRange.Visible = false;
            }
            catch (Exception) { }


        }
    #endregion
       




        #region Missile Select Visible



        #endregion

        //#region Fire Visible
        //private void btnFire_Click(object sender, EventArgs e)
        //{


        //    Random random = new Random((int)DateTime.Now.Ticks);
        //    int turn = eventC.On;

        //    switch (turn)
        //    {

        //        case 1: picboxShortRange.Visible = true;

        //            picBoxIncoming.Visible = false;
        //            picboxMediumRange.Visible = false;
        //            picBoxMissileFail.Visible = false;
        //            txtMisStatus.Text = "ENROUTE";
        //            break;

        //        //case 2: picBoxIncoming.Visible = true;

        //        //    picboxShortRange.Visible = false;
        //        //    picboxMediumRange.Visible = false;
        //        //    picBoxMissileFail.Visible = false;
        //        //    break;

        //        case 2: picboxMediumRange.Visible = true;

        //            picboxShortRange.Visible = false;
        //            picBoxIncoming.Visible = false;
        //            picBoxMissileFail.Visible = false;
        //            txtMisStatus.Text = "ENROUTE";
        //            break;

        //        //case 4: picBoxMissileFail.Visible = true;

        //        //    picboxShortRange.Visible = false;
        //        //    picBoxIncoming.Visible = false;
        //        //    picboxMediumRange.Visible = false;


        //            //break;
        //    }



        //    tDistance_Timer.Enabled = true;
        //    tAltitudeDecrement.Enabled = true;

        //    xWrite.WriteCOMEvent(0, 0, 0, 0, 0, configM.COMEventDir);
        //}

        //#endregion

        #region Abort Visible
        private void btnAbort_Click(object sender, EventArgs e)
        {


            picboxMediumRange.Visible = false;

            picboxShortRange.Visible = false;

            picBoxIncoming.Visible = false;
            picboxMediumRange.Visible = false;
            picBoxMissileFail.Visible = false;

            tAltitudeDecrement.Enabled = false;
            txtBogieSpeed.Clear();
            txtAltitude.Clear();
            txtDistance.Clear();
            txtMisStatus.Text = "Ready";


            tDistance_Timer.Enabled = false;
            tAltitudeDecrement.Enabled = false;
        #endregion
        }

        #region Distance Ticker




        private void tDistance_Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                int targetSpeed = Convert.ToInt32(txtBogieSpeed.Text);
                double targetDistance = Convert.ToDouble(txtDistance.Text);
                int missileMPH = 1000;
                double missileMPS = missileMPH * (1 / 60) * (1 / 60);
                double targetMPS = targetSpeed  * (1 / 60) * (1 / 60);

                distanceRemaining = targetDistance;

                distanceRemaining -= (missileMPS + targetMPS);

                //if (distanceRemaining <= 0)
                //{
                //    tAltitudeDecrement.Enabled = false;
                //    txtBogieSpeed.Text = "0";
                //    txtAltitude.Text = "0";
                //    txtDistance.Text = "0";
                //    txtMisStatus.Text = "Ready";
                //    MessageBox.Show("Superman HELP!!");
                //    //picBoxDestruction.Visible = true;                
                //}


                txtDistance.Text = Convert.ToString(distanceRemaining);
            }
            catch {}
        }

        private void tDistanceDecrement_Tick(object sender, EventArgs e)
        {
            try
            {

                int mph = Convert.ToInt32(txtBogieSpeed.Text);
                double distance = Convert.ToDouble(txtDistance.Text);
                double altitude = Convert.ToDouble(txtAltitude.Text);

                if (distance > 0)
                {
                    double droppingRate = altitude * 0.001;
                    altitude -= droppingRate;
                    txtAltitude.Text = String.Format("{0:0.00}", altitude);
                }


                double mps = mph * (1 / 60.0) * (1 / 60.0);
                distance -= mps;

                if (distance <= 0)
                {

                    txtDistance.Text = "0";
                    txtAltitude.Text = "0";
                    tAltitudeDecrement.Enabled = false;
                    // MessageBox.Show("Superman HELP!!");
                    //Application.Exit();

                }

                txtDistance.Text = String.Format("{0:0.00}", distance);
            }
            catch (Exception) { }

        #endregion
        }
    }
}

    
        
    


    

